package o;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.Typeface.Builder;
import android.graphics.fonts.FontVariationAxis;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.core.content.res.FontResourcesParserCompat.a;
import androidx.core.content.res.FontResourcesParserCompat.e;
import androidx.core.provider.FontsContractCompat;
import androidx.core.provider.FontsContractCompat.b;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.Map;

@RequiresApi(26)
@RestrictTo({d.ॱ})
public class fz extends gc {
    protected final Method ˊ;
    protected final Method ˋ;
    protected final Constructor ˎ;
    protected final Class ˏ;
    protected final Method ॱ;
    protected final Method ॱॱ;
    protected final Method ᐝ;

    public fz() {
        Method ॱ;
        Method ˋ;
        Method ˏ;
        Method ᐝ;
        Constructor constructor;
        Class cls;
        Throwable e;
        Method method = null;
        try {
            Class ˏ2 = ˏ();
            Constructor ˎ = ˎ(ˏ2);
            Method ˊ = ˊ(ˏ2);
            ॱ = ॱ(ˏ2);
            ˋ = ˋ(ˏ2);
            ˏ = ˏ(ˏ2);
            ᐝ = ᐝ(ˏ2);
            method = ˏ;
            ˏ = ˋ;
            ˋ = ॱ;
            ॱ = ˊ;
            constructor = ˎ;
            cls = ˏ2;
        } catch (ClassNotFoundException e2) {
            e = e2;
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class " + e.getClass().getName(), e);
            ᐝ = null;
            ˏ = null;
            ˋ = null;
            ॱ = null;
            constructor = null;
            cls = null;
            this.ˏ = cls;
            this.ˎ = constructor;
            this.ˋ = ॱ;
            this.ˊ = ˋ;
            this.ॱ = ˏ;
            this.ᐝ = method;
            this.ॱॱ = ᐝ;
        } catch (NoSuchMethodException e3) {
            e = e3;
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class " + e.getClass().getName(), e);
            ᐝ = null;
            ˏ = null;
            ˋ = null;
            ॱ = null;
            constructor = null;
            cls = null;
            this.ˏ = cls;
            this.ˎ = constructor;
            this.ˋ = ॱ;
            this.ˊ = ˋ;
            this.ॱ = ˏ;
            this.ᐝ = method;
            this.ॱॱ = ᐝ;
        }
        this.ˏ = cls;
        this.ˎ = constructor;
        this.ˋ = ॱ;
        this.ˊ = ˋ;
        this.ॱ = ˏ;
        this.ᐝ = method;
        this.ॱॱ = ᐝ;
    }

    private boolean ˊ() {
        if (this.ˋ == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        return this.ˋ != null;
    }

    @Nullable
    private Object ॱ() {
        try {
            return this.ˎ.newInstance(new Object[0]);
        } catch (IllegalAccessException e) {
            return null;
        } catch (InstantiationException e2) {
            return null;
        } catch (InvocationTargetException e3) {
            return null;
        }
    }

    private boolean ˏ(Context context, Object obj, String str, int i, int i2, int i3, @Nullable FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.ˋ.invoke(obj, new Object[]{context.getAssets(), str, Integer.valueOf(0), Boolean.valueOf(false), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), fontVariationAxisArr})).booleanValue();
        } catch (IllegalAccessException e) {
            return false;
        } catch (InvocationTargetException e2) {
            return false;
        }
    }

    private boolean ˏ(Object obj, ByteBuffer byteBuffer, int i, int i2, int i3) {
        try {
            return ((Boolean) this.ˊ.invoke(obj, new Object[]{byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Integer.valueOf(i3)})).booleanValue();
        } catch (IllegalAccessException e) {
            return false;
        } catch (InvocationTargetException e2) {
            return false;
        }
    }

    @Nullable
    protected Typeface ˎ(Object obj) {
        try {
            Array.set(Array.newInstance(this.ˏ, 1), 0, obj);
            return (Typeface) this.ॱॱ.invoke(null, new Object[]{r0, Integer.valueOf(-1), Integer.valueOf(-1)});
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e2) {
        }
        return null;
    }

    private boolean ॱ(Object obj) {
        try {
            return ((Boolean) this.ॱ.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException e) {
            return false;
        } catch (InvocationTargetException e2) {
            return false;
        }
    }

    private void ˊ(Object obj) {
        try {
            this.ᐝ.invoke(obj, new Object[0]);
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e2) {
        }
    }

    @Nullable
    public Typeface ॱ(Context context, e eVar, Resources resources, int i) {
        if (!ˊ()) {
            return super.ॱ(context, eVar, resources, i);
        }
        Object ॱ = ॱ();
        if (ॱ == null) {
            return null;
        }
        a[] ˊ = eVar.ˊ();
        int length = ˊ.length;
        int i2 = 0;
        while (i2 < length) {
            a aVar = ˊ[i2];
            if (ˏ(context, ॱ, aVar.ˊ(), aVar.ˎ(), aVar.ॱ(), aVar.ˏ() ? 1 : 0, FontVariationAxis.fromFontVariationSettings(aVar.ˋ()))) {
                i2++;
            } else {
                ˊ(ॱ);
                return null;
            }
        }
        if (ॱ(ॱ)) {
            return ˎ(ॱ);
        }
        return null;
    }

    @Nullable
    public Typeface ˋ(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull b[] bVarArr, int i) {
        Typeface ˎ;
        Throwable th;
        Throwable th2;
        if (bVarArr.length < 1) {
            return null;
        }
        if (ˊ()) {
            Map ˋ = FontsContractCompat.ˋ(context, bVarArr, cancellationSignal);
            Object ॱ = ॱ();
            if (ॱ == null) {
                return null;
            }
            Object obj = null;
            for (b bVar : bVarArr) {
                ByteBuffer byteBuffer = (ByteBuffer) ˋ.get(bVar.ˊ());
                if (byteBuffer != null) {
                    if (ˏ(ॱ, byteBuffer, bVar.ˎ(), bVar.ˏ(), bVar.ˋ() ? 1 : 0)) {
                        obj = 1;
                    } else {
                        ˊ(ॱ);
                        return null;
                    }
                }
            }
            if (obj == null) {
                ˊ(ॱ);
                return null;
            } else if (!ॱ(ॱ)) {
                return null;
            } else {
                ˎ = ˎ(ॱ);
                if (ˎ == null) {
                    return null;
                }
                return Typeface.create(ˎ, i);
            }
        }
        ParcelFileDescriptor openFileDescriptor;
        b ˎ2 = ˎ(bVarArr, i);
        try {
            openFileDescriptor = context.getContentResolver().openFileDescriptor(ˎ2.ˊ(), "r", cancellationSignal);
            th = null;
            if (openFileDescriptor != null) {
                try {
                    ˎ = new Builder(openFileDescriptor.getFileDescriptor()).setWeight(ˎ2.ˏ()).setItalic(ˎ2.ˋ()).build();
                    if (openFileDescriptor == null) {
                        return ˎ;
                    }
                    if (th != null) {
                        try {
                            openFileDescriptor.close();
                            return ˎ;
                        } catch (Throwable th3) {
                            th.addSuppressed(th3);
                            return ˎ;
                        }
                    }
                    openFileDescriptor.close();
                    return ˎ;
                } catch (Throwable th4) {
                    Throwable th5 = th4;
                    th4 = th2;
                    th2 = th5;
                }
            } else if (openFileDescriptor == null) {
                return null;
            } else {
                if (th4 != null) {
                    try {
                        openFileDescriptor.close();
                        return null;
                    } catch (Throwable th32) {
                        th4.addSuppressed(th32);
                        return null;
                    }
                }
                openFileDescriptor.close();
                return null;
            }
        } catch (IOException e) {
            return null;
        }
        throw th2;
        if (openFileDescriptor != null) {
            if (th4 != null) {
                try {
                    openFileDescriptor.close();
                } catch (Throwable th322) {
                    th4.addSuppressed(th322);
                }
            } else {
                openFileDescriptor.close();
            }
        }
        throw th2;
    }

    @Nullable
    public Typeface ॱ(Context context, Resources resources, int i, String str, int i2) {
        if (!ˊ()) {
            return super.ॱ(context, resources, i, str, i2);
        }
        Object ॱ = ॱ();
        if (ॱ == null) {
            return null;
        }
        if (!ˏ(context, ॱ, str, 0, -1, -1, null)) {
            ˊ(ॱ);
            return null;
        } else if (ॱ(ॱ)) {
            return ˎ(ॱ);
        } else {
            return null;
        }
    }

    protected Class ˏ() throws ClassNotFoundException {
        return Class.forName("android.graphics.FontFamily");
    }

    protected Constructor ˎ(Class cls) throws NoSuchMethodException {
        return cls.getConstructor(new Class[0]);
    }

    protected Method ˊ(Class cls) throws NoSuchMethodException {
        return cls.getMethod("addFontFromAssetManager", new Class[]{AssetManager.class, String.class, Integer.TYPE, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, FontVariationAxis[].class});
    }

    protected Method ॱ(Class cls) throws NoSuchMethodException {
        return cls.getMethod("addFontFromBuffer", new Class[]{ByteBuffer.class, Integer.TYPE, FontVariationAxis[].class, Integer.TYPE, Integer.TYPE});
    }

    protected Method ˋ(Class cls) throws NoSuchMethodException {
        return cls.getMethod("freeze", new Class[0]);
    }

    protected Method ˏ(Class cls) throws NoSuchMethodException {
        return cls.getMethod("abortCreation", new Class[0]);
    }

    protected Method ᐝ(Class cls) throws NoSuchMethodException {
        Method declaredMethod = Typeface.class.getDeclaredMethod("createFromFamiliesWithDefault", new Class[]{Array.newInstance(cls, 1).getClass(), Integer.TYPE, Integer.TYPE});
        declaredMethod.setAccessible(true);
        return declaredMethod;
    }
}
